package com.tiyende.credit.loop.ui.adapter

import android.annotation.SuppressLint
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.lib.common.adapter.CommonRecyclerViewAdapter
import com.lib.common.adapter.CommonViewHolderHelper
import com.tiyende.credit.loop.R
import com.tiyende.credit.loop.TiyendeApp
import org.json.JSONObject
import java.math.BigDecimal

/**
 * TiyendeCredit
 * @Author wwdeng
 * @CreateTime: 2024/11/19 10:43
 * @Annotation：订单列表适配器
 */
class TiyendeOrderRepayListAdapter(recyclerView: RecyclerView?) : CommonRecyclerViewAdapter<JSONObject>(recyclerView) {

    override fun getItemViewType(position: Int): Int {
        val model = getItem(position)
        val itemType = model.optString("itemType")
        return when(itemType){
            "emptyItemView" -> {
                R.layout.tiyende_activity_order_list_empty
            }
            else -> {
                R.layout.tiyende_fragment_order_repay_list_item
            }
        }
    }

    override fun setItemChildListener(helper: CommonViewHolderHelper, viewType: Int) {
        helper.setItemChildClickListener(R.id.btOrderRepay)
    }

    override fun fillData(helper: CommonViewHolderHelper, position: Int, model: JSONObject) {
        val itemType = model.optString("itemType")
        when (itemType){
            "emptyItemView" -> {//空
//                val textWithdraw = helper.getTextView(R.id.textWithdraw)
//                textWithdraw.paint.flags = Paint.UNDERLINE_TEXT_FLAG//下划线
//                textWithdraw.paint.isAntiAlias = true//抗锯齿
            }
            else -> {
                showTiyendeOrderRepayItemView(helper,position,model)
            }
        }
    }

    /**
     * 订单itemView
     */
    @SuppressLint("SetTextI18n")
    private fun showTiyendeOrderRepayItemView(helper: CommonViewHolderHelper, position: Int, model: JSONObject){
        //单位
        val unit = model.optString("currency")
        helper.setText(R.id.txtOrderRepayDueDate,String.format(getString(R.string.tiyende_order_repay_due_date),model.optString("repayTime")))
        helper.setText(R.id.txtOrderRepayIndex,String.format(getString(R.string.tiyende_order_repay_index),"${position+1}"))
        helper.setText(R.id.txtOrderRepayTotal,String.format(getString(R.string.tiyende_order_repay_total_repayment),unit))
        helper.setText(R.id.txtOrderRepayTotalAmount,BigDecimal(model.optString("repayAmount","0.00")).stripTrailingZeros().setScale(2).toPlainString())
        helper.setText(R.id.txtOrderRepayInterest,BigDecimal(model.optString("interest","0.00")).stripTrailingZeros().setScale(2).toPlainString())
        helper.setText(R.id.txtOrderRepayOverdueDays,model.optString("overdueDay","0"))
        helper.setText(R.id.txtOrderRepayOverduePenalty,BigDecimal(model.optString("overdueFee","0.00")).stripTrailingZeros().setScale(2).toPlainString())

        val serviceFee = BigDecimal(model.optString("processingFee","0.00")).stripTrailingZeros().setScale(2).toFloat()
        if (serviceFee > 0){
            helper.setVisibility(R.id.txtOrderRepayServiceFeeTitle, View.VISIBLE)
            helper.setVisibility(R.id.txtOrderRepayServiceFee, View.VISIBLE)
            helper.setText(R.id.txtOrderRepayServiceFee,"$serviceFee")
        } else {
            helper.setVisibility(R.id.txtOrderRepayServiceFeeTitle, View.INVISIBLE)
            helper.setVisibility(R.id.txtOrderRepayServiceFee, View.INVISIBLE)
        }
    }

    private fun getString(strId: Int): String{
        return mContext.getString(strId)
    }
}